<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>Data Bunker Login</title>
    <script src="site/site.js"></script>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
        integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    <link rel="stylesheet" href="site/style.css">
<script>
conf = loadUIConfiguration();
if (conf["custom_css_link"]) {
  document.write("<link rel='stylesheet' type='text/css' href='" + conf["custom_css_link"] +"' />");
}
</script>
</head>

<body>
  <div class="container col-md-6 pY-100">
    <script>
    conf = loadUIConfiguration();
    if (conf["LogoLink"]) {
      document.write("<center><div class='col-6'><img class='logo img-responsive' src='"+conf["LogoLink"]+"'></div></center>" );
    }
    </script>
    <div class="row">
      <div class="bigblock">
        <h4>Find my data</h4>
        <p>Choose a login method and input your login credentials:</p>
        <form id="loginform">
          <div class="form-group">
            <select onchange="changemethod(this);" class="custom-select" required id="keymethod">
              <option value="Admin">Root Token</option>
              <option value="Record">Record Token</option>
              <option selected value="Email">Email</option>
              <option value="Phone">Phone</option>
            </select>
          </div>
          <div class="form-group" id="email-conf-form" style="display:none;">
           <p>We might send your email using 3rd party service. Check the <b>Privacy policy</b> bellow.</p>
           <div class="form-check">
            <input type="checkbox" class="form-check-input" onclick="hidealert();" id="emailchk">
            <label class="form-check-label" for="emailchk">Confirm to allow sending access code using 3rd party service.</label>
           </div>
          </div>
          <div class="form-group" id="sms-conf-form" style="display:none;">
           <p>We might send you SMS using 3rd party service. Check the <b>Privacy policy</b> bellow.</p>
           <div class="form-check">
             <input type="checkbox" class="form-check-input" onclick="hidealert();" id="smschk">
             <label class="form-check-label" for="smschk">Confirm to allow sending access code using 3rd party service.</label>
            </div>
          </div>
          <div id="errmsg" class="alert alert-warning" role="alert" style="display:none;"></div>
          <div class="form-group">
            <input id="loginvalue" type="login" autocomplete="off" class="form-control" onclick="hidealert();"
              placeholder="Enter token...">
          </div>
          <div id="captcha-form">
           <img id="captchaurl" src="/v1/captcha/%CAPTCHAURL%" />
           <input id="captchavalue" type="login" autocomplete="off" class="form-control" onclick="hidealert();"
             style="float: right; width: 50%; margin-top: 12px;" placeholder="Enter captcha">
          </div>
          <div class="form-group">
            <div class="peers ai-c jc-sb fxw-nw">
              <div class="peer">
                    <div id="loading" style="display:none;">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" style="margin: auto; background: none; display: block; shape-rendering: auto;" width="110px" height="48px" viewBox="15 28 60 50" preserveAspectRatio="xMidYMid">
<defs>
  <clipPath id="progress-vacbif3f61-cp" x="0" y="0" width="100" height="100">
    <rect x="0" y="0" width="0" height="100">
      <animate attributeName="width" repeatCount="indefinite" dur="3.0303030303030303s" values="0;100;100" keyTimes="0;0.5;1"></animate>
      <animate attributeName="x" repeatCount="indefinite" dur="3.0303030303030303s" values="0;0;100" keyTimes="0;0.5;1"></animate>
    </rect>
  </clipPath>
</defs>
<path fill="none" stroke="#2196ef" stroke-width="2.25" d="M7.96875 36.625L92.03125 36.625A3.3437499999999996 3.3437499999999996 0 0 1 95.375 39.96875L95.375 60.03125A3.3437499999999996 3.3437499999999996 0 0 1 92.03125 63.375L7.96875 63.375A3.3437499999999996 3.3437499999999996 0 0 1 4.625 60.03125L4.625 39.96875A3.3437499999999996 3.3437499999999996 0 0 1 7.96875 36.625 Z"></path>
<path fill="#007bff" clip-path="url(#progress-vacbif3f61-cp)" d="M7.96875 37.75L92.03125 37.75A2.2187499999999996 2.2187499999999996 0 0 1 94.25 39.96875L94.25 60.03125A2.2187499999999996 2.2187499999999996 0 0 1 92.03125 62.25L7.96875 62.25A2.2187499999999996 2.2187499999999996 0 0 1 5.75 60.03125L5.75 39.96875A2.2187499999999996 2.2187499999999996 0 0 1 7.96875 37.75 Z"></path>
<!-- [ldio] generated by https://loading.io/ --></svg>
                    </div>
              </div>
              <div class="peer"><button onclick="return submitbtn();"
                  class="btn btn-primary">Login</button></div>
            </div>
          </div>
        </form>
      </div>
    </div>
  <script>displayFooterLinks();</script>
</div>
<script>
var lookupTimer;
var lookupCount;
var captchaCode = "%CAPTCHAURL%";

function lookupUser(url) {
  if (lookupCount > 0) {
    // try to login with user again
    var xhr10 = new XMLHttpRequest();
    xhr10.open('GET', url);
    xhr10.onload = function () {
      if (xhr10.status === 200) {
         var data = JSON.parse(xhr10.responseText);
         if (data.status == "error" && data.result && data.result == "captcha-error") {
           showalert("Captcha error. Refresh the page and try again.");
         } else if (data.status == "ok") {
           document.location = "/site/login.html";
           return;
         }
      } else if (xhr10.status === 403) {
        var data = JSON.parse(xhr10.responseText);
        if (data.status == "error" && data.captchaurl) {
          if (lookupCount === 0) {
            var captchaurl = document.getElementById('captchaurl');
            captchaCode = data.captchaurl;
          }
        }
      }
    }
    lookupCount = lookupCount - 1;
    xhr10.send();
  } else {
    clearTimeout(lookupTimer);
    captchaurl.src = '/v1/captcha/' + captchaCode;
    var loading = document.getElementById('loading');
    loading.style.display = "none";
    showalert("Failed to find user record. User record might be deleted.");
  }
}
function isUUID(uuid) {
  let s = "" + uuid;
  s = s.match('^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$');
  if (s === null) {
    return false;
  }
  return true;
}
function showalert(msg) {
  var errmsg = document.getElementById('errmsg');
  errmsg.style.display = "block"
  errmsg.innerHTML = msg;
}
function hidealert() {
  var errmsg = document.getElementById('errmsg');
  errmsg.style.display = "none";
  errmsg.innerHTML = "";
}
function changemethod(obj) {
  var value = obj.value;
  if (!value) {
    return false;
  }
  changemethodnow(value);
}
function changemethodnow(value) {
  var login = document.getElementById('loginvalue');
  var smsform = document.getElementById('sms-conf-form');
  var emailform = document.getElementById('email-conf-form');
  var captchaform = document.getElementById('captcha-form');
  var smschk = document.getElementById('smschk');
  var emailchk = document.getElementById('emailchk');
  var loading = document.getElementById('loading');
  if (!login || !smsform || !emailform ||
    !smschk || !emailchk || !loading) {
    return false;
  }
  hidealert();
  smschk.checked = false;
  emailchk.checked = false;
  value = value.toLowerCase();
  valueDisplay = value;
  if (valueDisplay === "admin") {
    valueDisplay = "admin token";
  }
  var key = login.placeholder = "Enter " + valueDisplay + "...";
  if (value == "email") {
    smsform.style.display = "none";
    emailform.style.display = "block";
    captchaform.style.display = "block";
  } else if (value == "phone") {
    smsform.style.display = "block";
    emailform.style.display = "none";
    captchaform.style.display = "block";
  } else {
    smsform.style.display = "none";
    emailform.style.display = "none";
    captchaform.style.display = "none";
  }
}
function submitbtn() {
  var login = document.getElementById('loginvalue');
  var captcha = document.getElementById('captchavalue');
  var smschk = document.getElementById('smschk');
  var emailchk = document.getElementById('emailchk');
  var keymethod = document.getElementById('keymethod');

  if (!login || !smschk || !emailchk || !keymethod) {
    return false;
  }
  var key = login.value;
  if (!key) {
    return false;
  }
  code = captcha.value;
  var kkk = keymethod.options[keymethod.selectedIndex].value;
  if ((kkk == "Email" && emailchk.checked == false) ||
    (kkk == "Phone" && smschk.checked == false)) {
    showalert("We can not send you access code without your consent.");
    return false;
  }
  if ((kkk == "Email" || kkk == "Phone") && !code){
    showalert("Captcha code is missing");
    return false;
  }
  if (kkk == "Record" && isUUID(key) == true) {
    var xhr = new XMLHttpRequest();
    xhr.open('GET', "/v1/get/" + key);
    xhr.onload = function () {
      if (xhr.status === 200) {
        var data = JSON.parse(xhr.responseText);
        if (data && data.status && data.status == "ok") {
          window.localStorage.setItem('record', key);
          window.localStorage.setItem('type', data.type);
          if (data.data) {
            document.location = "/site/display-data.html";
          } else {
            document.location = "/site/admin-events.html";
          }
        }
      }
    };
    xhr.send();
  } else if (kkk == "Admin" && (key == "DEMO" || isUUID(key) == true)) {
    var xhr = new XMLHttpRequest();
    xhr.open('GET', "/v1/userapps");
    xhr.setRequestHeader("X-Bunker-Token", key)
    xhr.setRequestHeader("Content-type", "application/json");
    xhr.onload = function () {
      if (xhr.status === 200) {
        var data = JSON.parse(xhr.responseText);
        if (data && data.status && data.status == "ok") {
          window.localStorage.setItem('xtoken', key);
          window.localStorage.setItem('type', data.type);
          document.location = "/site/admin-view-requests.html";
        }
      }
    };
    xhr.send();
  } else if (kkk == "Email" && key.indexOf('@') > 0) {
    window.localStorage.setItem('login', key);
    var xhr0 = new XMLHttpRequest();
    xhr0.open('POST', "/v1/agreement/core-send-email-on-login/email/" + encodeURI(key) + "");
    xhr0.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
    xhr0.onload = function () {
      if (xhr0.status === 200) {
        var xhr = new XMLHttpRequest();
        var url = "/v1/prelogin/email/" + encodeURI(key) + "/" + code + "/" + captchaCode;
        xhr.open('GET', url);
        xhr.onload = function () {
          if (xhr.status === 200) {
             var data = JSON.parse(xhr.responseText);
             if (data.status == "error") {
               if (data.result && data.result == "captcha-error") {
                 showalert("Captcha error. Refresh the page and try again.");
               } else {
                 showalert("Login error. Try again.");
               }
               return;
             } else if (data.status == "ok") {
               document.location = "/site/login.html";
               return;
             }
          }
          // lets wait for the results
          var loading = document.getElementById('loading');
          loading.style.display = "block";
          lookupCount = 3;
          lookupTimer = setInterval(lookupUser, 5000, url);
        }
        xhr.send();
      }
    }
    xhr0.send();
  } else if (kkk == "Phone") {
    window.localStorage.setItem('login', key);
    var xhr0 = new XMLHttpRequest();
    xhr0.open('POST', "/v1/agreement/core-send-sms-on-login/phone/" + encodeURI(key) + "");
    xhr0.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
    xhr0.onload = function () {
      if (xhr0.status === 200) {
        var xhr = new XMLHttpRequest();
        xhr.open('GET', "/v1/prelogin/phone/" + encodeURI(key) + "/" + code + "/" + captchaCode);
        xhr.onload = function () {
          if (xhr.status === 200) {
             var data = JSON.parse(xhr.responseText);
             if (data.status == "error" && data.result && data.result == "captcha-error") {
               showalert("Captcha error. Refresh the page and try again.");
               return;
             } else if (data.status == "ok") {
               document.location = "/site/login.html";
               return;
             }
          }
        }
        xhr.send();
      }
    }
    xhr0.send();
  } else {
    if (kkk == "Email" && key.indexOf('@') == -1) {
      showalert("Bad email address format");
    } else {
      showalert("Bad input value!");
    }
  }
  return false;
}
(function() {
  changemethodnow('Email');
})();
</script>
</body>
